

package Game.Objects;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import java.util.Random;

/**
 * A rock, a non movable object
 * Compiler: jdk1.6.0_14 <br />
 * IDE: NetBeans 6.7.1 <br />
 * @author Andrew Smith <br />
 */
public class Rock
{
    /** maximum bounds for a rock to be generated */
    private static final int MAX_ROCK_BOUNDS = 100;
    /** the rock object as a shape */
    private Polygon rock;
    /** the x location */
    private int xPos;
    /** the y location */
    private int yPos;

    /**
     * Creates a random looking rock, at the coordinates given
     * @param x the x coordinate of the rock
     * @param y the y coordinate of the rock
     */
    public Rock(int x, int y)
    {
        this.xPos = x; this.yPos = y;
        generateRock();
    }

    /**
     * Checks to see if it contains the x and y coordinates
     * @param x the x coordinate
     * @param y the y coordinate
     * @return true if it contains them 
     */
    public boolean contains(int x, int y)
    {   return rock.contains(x, y);
    }

    /**
     * Checks to see if it contains the point passed to it
     * @param p the point to check
     * @return true if it contains this point
     */
    public boolean contains(Point p)
    {   return rock.contains(p);
    }

    /**
     * Generates the rock
     */
    private void generateRock()
    {
        int minX = xPos - (MAX_ROCK_BOUNDS/2);
        int minY = yPos - (MAX_ROCK_BOUNDS/2);
        
        Random rangen = new Random();
        //can have a maximum of 6 points, minimum 3 (3+3)
        int numPoints = rangen.nextInt(3) + 3;
        int[] xPositions = new int[numPoints];
        int[] yPositions = new int[numPoints];

        for(int i=0; i<numPoints; i++)
        {
            xPositions[i] = rangen.nextInt(MAX_ROCK_BOUNDS) + minX;
            yPositions[i] = rangen.nextInt(MAX_ROCK_BOUNDS) + minY;
        }

        rock = new Polygon(xPositions, yPositions, numPoints);
    }


    /**
     * Draws this rock on the current graphics
     * @param g
     */
    public void draw(Graphics g)
    {
        g.setColor(Color.LIGHT_GRAY);
        g.fillPolygon(rock);
    }
}
